/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package se.sll.service.catalogue.cli;

import lombok.extern.slf4j.Slf4j;
import org.apache.camel.Exchange;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.model.dataformat.JsonLibrary;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import se.sll.service.catalogue.common.RestClientHelper;
import se.sll.service.catalogue.common.property.CatalogProperties;
import se.sll.service.catalogue.common.rest.model.CodeSystemCode;
import se.sll.service.catalogue.common.rest.request.CreateOfferRequest;
import se.sll.service.catalogue.importer.extract.VTGetProcessor;
import se.sll.service.catalogue.importer.transform.ToCapacity;
import se.sll.service.catalogue.importer.transform.ToOffers;
import se.sll.service.catalogue.importer.transform.ToProducts;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;
import java.io.IOException;
import java.util.concurrent.Future;

/**
 *
 * @author khaleddaham
 */
@Slf4j
public class EKProductCodes {

    private static final String REST_URL_PRODUCT = CatalogProperties.getProperty(CatalogProperties.NAME.PRODUCT_BASE_URL) + "/products/";
    private static final String REST_URL_CAPACITY = "http://localhost:8080/capacity/capacity/";
    private static final String REST_URL_OFFERS = CatalogProperties.getProperty(CatalogProperties.NAME.OFFERS_BASE_URL) + "/units/";

    private static VTGetProcessor vtGetProcessor;
    private static ToCapacity toCapacity;
    private static ToOffers toOffers;
    private static ToProducts toProducts;
    private static Document doc = null;
    Future<Object> vtGetFuture = null;
    private static DefaultCamelContext ctx = null;
    private static ProducerTemplate template = null;

    private void readVtGet(String file) {
        Future<Object> future = template.asyncRequestBody("direct:start:vtget", file);
//        String response = template.extractFutureBody(future, String.class)
    }

    private static void loadRoutes() throws Exception {
        ctx = new DefaultCamelContext();
        ctx.addRoutes(new RouteBuilder() {
            public void configure() {
                from("direct:start:codes:add")
                        .setHeader(Exchange.HTTP_METHOD, constant("POST"))
                        .setHeader(Exchange.CONTENT_TYPE, constant("application/json; charset=UTF-8"))
                        .setHeader(Exchange.ACCEPT_CONTENT_TYPE, constant("application/json; charset=UTF-8"))
                        .log("EK product code update started.")
                        .split().body().parallelProcessing()
                        .marshal().json(JsonLibrary.Jackson)
                        .to(CatalogProperties.getProperty(CatalogProperties.NAME.PRODUCT_BASE_URL) + "/capacity");

            }
        });
        ctx.start();
    }

    /**
     * Standalone runner for importing data to offer/products/capacity/idmap
     *
     * @param args
     * @throws XPathExpressionException
     * @throws ParserConfigurationException
     * @throws SAXException
     * @throws IOException
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public static void main(String[] args) throws XPathExpressionException, ParserConfigurationException, SAXException, IOException, Exception {
        addJiri();

    }

    private static void addJiri() throws Exception {
        CreateOfferRequest offer = new CreateOfferRequest();
        offer.setHsaID("SE5565189692-101J6");
        offer.setRegionalUnitId("SE5565189692-101J6");
        String url = CatalogProperties.getProperty(CatalogProperties.NAME.OFFERS_BASE_URL)  + "/units";
        log.info("Adding Jiri offer.");
        RestClientHelper.sendJSONObject(url, offer, 204);
        RestClientHelper.postQueryParam(url + "/SE5565189692-101J6/product/", "product", "1", 204);
    }

    private static void addEk() throws Exception {
        CodeSystemCode cs = new CodeSystemCode();
        cs.setOid("1.2.752.97.3.1.11");
        String url = CatalogProperties.getProperty(CatalogProperties.NAME.PRODUCT_BASE_URL);
        log.info("Adding EK product codes.");
        //        cs.setCode("1303");
//        RestClientHelper.putJSONObject(url + "/products/153/add/code", cs, 204);
        cs.setCode("1706");
        RestClientHelper.putJSONObject(url + "/products/95/add/code", cs, 204);
        cs.setCode("1705");
        RestClientHelper.putJSONObject(url + "/products/92/add/code", cs, 204);
        cs.setCode("1703");
        RestClientHelper.putJSONObject(url + "/products/94/add/code", cs, 204);
        cs.setCode("1702");
        RestClientHelper.putJSONObject(url + "/products/91/add/code", cs, 204);
        cs.setCode("1704");
        RestClientHelper.putJSONObject(url + "/products/93/add/code", cs, 204);
    }
}
